package org.jeecg.modules.course.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.course.entity.AstuCourse;
import org.jeecg.modules.course.mapper.AstuCourseMapper;
import org.jeecg.modules.course.service.IAstuCourseService;
import org.jeecg.modules.experiment.entity.AstuExperiment;
import org.jeecg.modules.experiment.mapper.AstuExperimentMapper;
import org.jeecg.modules.tea_stu_course.entity.AstuTeastuCourse;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 课程表
 * @Author: jeecg-boot
 * @Date:   2021-10-22
 * @Version: V1.0
 */
@Service
public class AstuCourseServiceImpl extends ServiceImpl<AstuCourseMapper, AstuCourse> implements IAstuCourseService {

    @Resource
    private AstuCourseMapper astuCourseMapper;
    @Resource
    private AstuExperimentMapper astuExperimentMapper;
    @Override
    public String selectIdByCoures(String courseName) {
        return astuCourseMapper.selectIdByCoures(courseName);
    }

    @Override
    public List<AstuCourse> selectAll() {
        return astuCourseMapper.selectList(null);
    }

    @Override
    public List<AstuTeastuCourse> selectStuNumberAndStuName() {
        return astuCourseMapper.selectStuNumberAndStuName();
    }

    @Override
    @Transactional
    public void saveMain(AstuCourse astuCourse, List<AstuExperiment> astuExperimentList) {
        astuCourseMapper.insert(astuCourse);
        if(astuExperimentList!=null && astuExperimentList.size()>0) {
            for(AstuExperiment entity:astuExperimentList) {
                //外键设置
                entity.setCourseId(astuCourse.getId());
                astuExperimentMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional
    public void updateMain(AstuCourse astuCourse,List<AstuExperiment> astuExperimentList) {
        astuCourseMapper.updateById(astuCourse);

        //1.先删除子表数据
        astuExperimentMapper.deleteByMainId(astuCourse.getId());

        //2.子表数据重新插入
        if(astuExperimentList!=null && astuExperimentList.size()>0) {
            for(AstuExperiment entity:astuExperimentList) {
                //外键设置
                entity.setCourseId(astuCourse.getId());
                astuExperimentMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional
    public void delMain(String id) {
        astuExperimentMapper.deleteByMainId(id);
        astuCourseMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for(Serializable id:idList) {
            astuExperimentMapper.deleteByMainId(id.toString());
            astuCourseMapper.deleteById(id);
        }
    }

    @Override
    public String selectCourseNumberByid(String id) {
        return astuCourseMapper.selectCourseNumberByid(id);
    }

    @Override
    public List<AstuCourse> selectAllCourseNum() {
        return astuCourseMapper.selectAllCourseNum();
    }

    @Override
    public String selectIdByCouseNum(String courseNumber) {
        return astuCourseMapper.selectIdByCouseNum(courseNumber);
    }

    @Override
    public AstuCourse selectInfoByCourseNum(String courseNumber) {
        return astuCourseMapper.selectInfoByCourseNum(courseNumber);
    }
}
